package com.np.xxpermissions1.permission;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class DialogFragmentFullscreen extends DialogFragment {

    protected int m_gravity = Gravity.CENTER;
    protected int m_offset_x = 0;
    protected int m_offset_y = 0;
    protected int m_width = WindowManager.LayoutParams.WRAP_CONTENT;
    protected int m_height = WindowManager.LayoutParams.WRAP_CONTENT;
    protected float m_alpha = 1.0f;
    protected float m_dimAmount = 0.0f;

    public DialogFragmentFullscreen() {
        super();
    }

    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // 这句必须,否则将闪烁
        if (dialog.getWindow() != null) {
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }
        // 不显示标题栏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    /**
     * 在此设置窗口位置
     */
    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = super.getDialog();
        if (dialog != null) {
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);

            Window window = dialog.getWindow();
            if (window != null) {
                //Utils.Display.setNavigationBarAndStatusBar(window, true);

                WindowManager.LayoutParams params = window.getAttributes();
                {
                    params.gravity = m_gravity;
                    params.x = m_offset_x;
                    params.y = m_offset_y;
                    params.width = m_width;
                    params.height = m_height;
                    params.alpha = m_alpha;
                    params.dimAmount = m_dimAmount;
                }
                window.setAttributes(params);
            }

            // 监听系统返回键
            dialog.setOnKeyListener((dialog1, keyCode, keyEvent) -> {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    this.onKeyBack();
                    return true;
                }
                return false;
            });
        }
    }

    /**
     * 在此加载布局
     */
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * 在此查找窗口控件并实现功能
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    /**
     * 在此接收返回结果
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 按系统返回键
     */
    public void onKeyBack() {
        super.dismiss();
    }

    /**
     * 在此消毁
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
